/**
 *
 * @param {*} params 表格查询参数
 * @param {*} pageOpt 页码参数
 * @returns
 */
import { yuanToFen } from '@/utils'
export function resetParams(params, pageOpt) {
  let _params = params
  let _curKey = {}
  Object.keys(_params).forEach(itemKey => {
    if (itemKey.match(/[:|,]/g)) {
      const [curType, curFiled] = itemKey.split(':')
      if (curType === 'picker') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey]) // 删除params中的时间属性
        // console.log('curKey', _curKey)
        const [v1, v2] = _params[itemKey] || [null, null]
        const [k1, k2] = curFiled.split(',')
        console.log('k1, k2', k1, k2)
        _params = {
          ..._curKey,
          [k1]: v1 ? new Date(v1).getTime() : null,
          [k2]: v2 ? new Date(v2).getTime() : null
        }
      }
      if (curType === 'cascader') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey]) // 删除params中的时间属性
        // console.log('curKey', _curKey)
        const [v1, v2, v3] = _params[itemKey] || [null, null, null]
        const [k1, k2] = curFiled.split(',')
        console.log('k1, k2', k1, k2, v1)
        _params = {
          ..._curKey,
          [k1]: v2,
          [k2]: v3
        }
      }
      if (curType === 'cascaderThree') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey]) // 删除params中的时间属性
        // console.log('curKey', _curKey)
        const [v1, v2, v3] = _params[itemKey] || [null, null, null]
        const [k1, k2, k3] = curFiled.split(',')
        console.log('k1, k2', k1, k2, v1)
        _params = {
          ..._curKey,
          [k1]: v1,
          [k2]: v2,
          [k3]: v3
        }
      }
      if (curType === 'integer') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey])
        _params = {
          ..._curKey,
          [curFiled]: ~~_params[itemKey] || null
        }
      }
      if (curType === 'string') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey])
        _params = {
          ..._curKey,
          [curFiled]: _params[itemKey] || null
        }
      }
      if (curType === 'findTime') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey]) // 删除params中的时间属性
        // console.log('curKey', _curKey)
        const [k1, k2] = curFiled.split(',')
        const v1 = _params[k1]
        const v2 = _params[k2]
        const [time1, time2] = v2 && v2.length ? v2 : ['00:00:00', '23:59:59']
        _params = {
          ..._curKey,
          [k1]: v1 ? new Date(v1 + ' ' + time1).getTime() : null,
          [k2]: v1 ? new Date(v1 + ' ' + time2).getTime() : null
        }
      }
      if (curType === 'price') {
        // eslint-disable-next-line no-undef
        _curKey = _.omit(_params, [itemKey])
        _params = {
          ..._curKey
        }
        const KeyArr = curFiled.split(',')
        KeyArr.forEach(key => {
          if (_params[key]) {
            _params[key] = yuanToFen(_params[key])
          }
        })
      }
    }
  })
  return { ...pageOpt, ..._params }
}

// 汇总数据转成指定格式
export function resetTableSummary(obj = {}, nameMap = {}) {
  const summaryData = []
  for (const key in nameMap) {
    const item = {
      name: nameMap[key],
      value: obj[key]
    }
    summaryData.push(item)
  }
  return summaryData
}
